<?php

namespace App\Repositories;

use App\Models\Post;
use InfyOm\Generator\Common\BaseRepository;

class PostRepository extends BaseRepository
{
    /**
     * @var array
     */
    protected $fieldSearchable = [
        'category_id',
        'title',
        'slug',
        'summary',
        'thumb',
        'content',
        'comment_count',
        'view_count',
        'favorite_count',
        'published',
        'published_at'
    ];

    /**
     * @return Post
     */
    public function model()
    {
        return Post::class;
    }


    protected function getNextArticleId($id)
    {
        return Post::where('id', '>', $id)->min('id');
    }

    protected function getPrevArticleId($id)
    {
        return Post::where('id', '<', $id)->max('id');
    }

    /**
     * @param $ids
     * @return bool|null
     * @throws \Exception
     */
    public function batchSoftDelete($ids)
    {
        return Post::where('id', 'in', $ids)->delete();
    }

    public function batchPublishedOrNo($ids, $published = true)
    {
        if ($ids && is_array($ids)) {
            if ($published) {
                return Post::where('id', 'in', $ids)->update(['published' => 1]);
            }else{
                return Post::where('id', 'in', $ids)->update(['published' => 2]);
            }
        }
        return false;
    }

    public function batchHotOrNo($ids, $hot = true)
    {

        if ($ids && is_array($ids)) {
            if ($hot) {
                return Post::where('id', 'in', $ids)->update(['hot' => 1]);
            }else{
                return Post::where('id', 'in', $ids)->update(['hot' => 2]);
            }
        }
        return false;
    }


    public function incrementPostCount(){

    }
}
